package com.framework.wz.wutong.base;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Iterator;
import java.util.Map;

public class BasePreference {
    private Context                  context;
    private SharedPreferences        sp;
    private SharedPreferences.Editor editor;
    private String FILE_NAME = "data";

    protected BasePreference(Context context) {
        this.context = context;
        sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    protected void setString(String key, String value) {
        editor.putString(key, value).apply();
    }

    protected String getString(String key) {
        return sp.getString(key, null);
    }

    protected void setBoolean(String key, boolean value) {
        editor.putBoolean(key, value).apply();
    }

    protected boolean getBoolean(String key) {
        return sp.getBoolean(key, false);
    }

    protected void setInt(String key, int value) {
        editor.putInt(key, value).apply();
    }

    protected int getInt(String key) {
        return sp.getInt(key, 0);
    }

    protected void setStrings(Map<String, String> strMap) {

        Iterator<Map.Entry<String, String>> iterator = strMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            editor.putString(entry.getKey(), entry.getValue());
        }
        editor.apply();
    }

    protected void setInts(Map<String, Integer> strMap) {

        Iterator<Map.Entry<String, Integer>> iterator = strMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            editor.putInt(entry.getKey(), entry.getValue());
        }
        editor.apply();
    }

    protected void setBooleans(Map<String, Boolean> strMap) {

        Iterator<Map.Entry<String, Boolean>> iterator = strMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Boolean> entry = iterator.next();
            editor.putBoolean(entry.getKey(), entry.getValue());
        }
        editor.apply();
    }

    public Boolean contains(String key) {
        return sp.contains(key);
    }

    public void clear() {
        editor.clear().apply();
    }
}
